﻿
window.Pagination_ToPage = function (url, index, pagename) {
	if (pagename) {
		this.location.href = url + "&" + pagename + "=" +index;
	} else {
		this.location.href = url + index;
	}
}

window.Pagination_Prev = function (url, index, pagename) {
	if (pagename) {
		this.location.href = url + "&" + pagename + "=" + (index - 1);
	} else {
		this.location.href = url + (index - 1);
	}
}

window.Pagination_Next = function (url, index, pagename) {
	if (pagename) {
		this.location.href = url + "&" + pagename + "=" + (index + 1);
	} else {
		this.location.href = url + (index + 1);
	}
}

function pagination(params)
{
	// 当前分页
	var page = params.page;

	// 索引关键字
	var pagename = params.name;

	// 地址
	var url = params.url;

	// 总页数
	var total = params.total;

	// 最多显示几个按钮页数
	var pageMaxNum = 5;

	if (total === 1) {
		return;
	}

	// 上一个
	var html = (page === 1 ? '' : '<a onclick="window.Pagination_Prev(\'' + url + '\',' + page + ',\'' + pagename + '\')" class="btn">Prev</a> &nbsp;');

	// 构造按钮
	var item = function (index) {
		return '<a onclick="window.Pagination_ToPage(\'' + url + '\',' + index + ',\'' + pagename + '\')" class="' + (page === index ? "btn active" : "btn") + '">' + index + '</a> &nbsp;';
	}

	var defaultIndex = (page >= pageMaxNum ? (page - Math.floor(pageMaxNum / 2)) : 1);

	// 拼接数据
	for (var i = defaultIndex; i < pageMaxNum + defaultIndex; i++) {
		if (i > total) {
			break;
		}
		html += item(i);
	}

	// 下一页
	html += page === total ? '' : '<a onclick="window.Pagination_Next(\'' + url + '\',' + page + ',\'' + pagename + '\')" class="btn">Next</a>';


	$("#pagination").html(html);
}